import cv2
import pyautogui
from ultralytics import YOLO
import time
import sys
import os
import subprocess  # 确保导入 subprocess 模块
from moveFb模块 import movefb
import win32gui
import 检测框模块
from find_class_module import find_class_and_perform_action
from yolo2 import 主程序
from yolo2.find_and_perform_action_module import find_and_perform_action
from yolo2.yolo找图模块 import click_target_image
from yolo2.循环找图模块 import run_action_condition_found, run_action_condition_not_found


def draw_detection_boxes(screenshot, results):
    for result in results:
        boxes = result.boxes.cpu().numpy()
        for box in boxes:
            class_id = int(box.cls[0])
            x1, y1, x2, y2 = box.xyxy[0].astype(int)
            center_x = int((x1 + x2) / 2)
            center_y = int((y1 + y2) / 2)

            # 在图像上绘制检测框
            cv2.rectangle(screenshot, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.putText(screenshot, f"Class {class_id}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

    return screenshot


def assign_tasks():
    # 禁用 pyautogui 的安全保护机制
    pyautogui.FAILSAFE = False

    # 加载 YOLOv8n 模型并指定使用GPU
    model = YOLO(r"h:\yol\yolo\v11\runs\detect\train41\weights\best.pt").to('cuda')

    hwnd = 检测框模块.find_window()
    if hwnd is None:
        print("未找到标题中包含'梦幻西游'的窗口")
        return

    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    window_width = right - left
    window_height = bottom - top

    if hasattr(检测框模块, 'MODULE_LOADED') and 检测框模块.MODULE_LOADED:
        print("检测框模块已正确加载")
    else:
        print("检测框模块加载失败")
    print("分任务模块启动成功，正在分配任务")

    # 杀妖任务逻辑
    def action_kill_monster():
        print("启动杀妖任务")

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')
            time.sleep(1)

        find_class_and_perform_action(hwnd, model, 1, action_a)

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-110, offset_y=50,
               置信度=0.3, click_type='left')

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=1, offset_x=0, offset_y=0,
                   置信度=0.3, click_type='left')

        find_class_and_perform_action(hwnd, model, 151, action_a)

        def action2():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')

        find_and_perform_action(hwnd, model, 5, action2)
        主程序.main()
        sys.exit()

    # 招贤任务逻辑
    def action_recruit():
        print("启动招贤任务")

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
               offset_y=-12, 置信度=0.3, click_type='left')
        time.sleep(0.1)

        def action_b():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-100,
                   offset_y=50, 置信度=0.3, click_type='left')

        find_class_and_perform_action(hwnd, model, 148, action_b)

        def action_c():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')

        find_class_and_perform_action(hwnd, model, 1, action_c)

        def action_A():
            print("等待3秒截图")
            time.sleep(3)
            # 保存游戏窗口截图
            screenshot = 检测框模块.get_window_screenshot(hwnd)
            save_dir = r"h:\yol\ultralytics\datasets\mycp\images"
            if not os.path.exists(save_dir):
                os.makedirs(save_dir)
            save_path = os.path.join(save_dir, f"screenshot_{int(time.time())}.png")
            cv2.imwrite(save_path, screenshot)
            print(f"游戏窗口截图已保存到 {save_path}")

            def action1():
                movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=40, offset_x=0,
                       offset_y=-10, 置信度=0.3, click_type='left', success_image=151)

            find_and_perform_action(hwnd, model, 57, action1)

            def action2():
                movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=38, offset_x=0,
                       offset_y=-10, 置信度=0.3, click_type='left', success_image=151)

            find_and_perform_action(hwnd, model, 41, action2)

            def action3():
                movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=39, offset_x=0,
                       offset_y=-10, 置信度=0.3, click_type='left', success_image=151)

            find_and_perform_action(hwnd, model, 58, action3)

            def action4():
                movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=37, offset_x=0,
                       offset_y=-10, 置信度=0.3, click_type='left', success_image=151)

            find_and_perform_action(hwnd, model, 59, action4)

            def action5():
                movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=22, offset_x=0,
                       offset_y=-10, 置信度=0.3, click_type='left', success_image=151)

            find_and_perform_action(hwnd, model, 56, action5)

        pyautogui.hotkey('alt', 'e')
        pyautogui.hotkey('alt', 'e')
        find_class_and_perform_action(hwnd, model, 151, action_A)

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')

        主程序.main()
        sys.exit()

    # 送信任务逻辑
    def action_send_letter():
        print("启动送信任务")

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left', mouse_shape_image=r'F:\zdh\yolo2\png\fb2.png')

        def action_b():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')

        find_class_and_perform_action(hwnd, model, 70, action_b)

        def action_c():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=70, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')

        find_class_and_perform_action(hwnd, model, 5, action_c)

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')

        sys.exit()

    # 除盗任务逻辑
    def action_chudao():
        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')

        find_class_and_perform_action(hwnd, model, 50, action_a)

        def action_b():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=50, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')

        find_class_and_perform_action(hwnd, model, 1, action_b)

        def action_c():
            print("秒过去")
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=1, offset_x=60, offset_y=0,
                   置信度=0.3, click_type='left')
            pyautogui.moveRel(-60, 0)
            screenshot = 检测框模块.get_window_screenshot(hwnd)
            save_dir = r"h:\yol\ultralytics\datasets\mycp\images"
            if not os.path.exists(save_dir):
                os.makedirs(save_dir)
            save_path = os.path.join(save_dir, f"screenshot_{int(time.time())}.png")
            cv2.imwrite(save_path, screenshot)
            print(f"游戏窗口截图已保存到 {save_path}")
            pyautogui.click()
            pyautogui.click()
            pyautogui.moveRel(60, 0)
            pyautogui.hotkey('alt', 'q')
            pyautogui.hotkey('alt', 'q')

        find_class_and_perform_action(hwnd, model, 151, action_c)

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')
        主程序.main()
        sys.exit()

    # 任务分配
    find_and_perform_action(hwnd, model, 31, action_kill_monster)
    find_and_perform_action(hwnd, model, 32, action_recruit)
    find_and_perform_action(hwnd, model, 52, action_send_letter)
    find_and_perform_action(hwnd, model, 42, action_chudao)

    print("什么都没找到，退出")
    sys.exit()


# 添加 if __name__ == "__main__" 代码块
if __name__ == "__main__":
    # 这里编写用于测试的代码
    assign_tasks()